package com.wandousoushu.jiusen.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wandousoushu.jiusen.MyApplication;
import com.wandousoushu.jiusen.R;
import com.wandousoushu.jiusen.bean.PayRule;
import com.wandousoushu.jiusen.booksource.data.entities.SearchBook;

import java.util.List;

public class PayRuleAdapter extends RecyclerView.Adapter<PayRuleAdapter.Vh>  {

    private Context mContext;
    private List<PayRule> mPayRuleList;
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private View.OnClickListener mOnClickListener;
    private int curPosition=0;

    public PayRuleAdapter(Context mContext, List<PayRule> payRuleList) {
        this.mContext = mContext;
        this.mPayRuleList=payRuleList;
        mInflater = LayoutInflater.from(mContext);
        this.notifyDataSetChanged();
        mOnClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Object tag = view.getTag();
                if(tag!=null){
                    int position = (int) tag;
                    if (mActionListener != null) {
                        mActionListener.onItemClick(mPayRuleList.get(position));
                    }
                    curPosition=position;
                    notifyDataSetChanged();
                }
            }
        };
    }

    public void setActionListener(ActionListener listener) {
        mActionListener = listener;
    }

    @Override
    public PayRuleAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_payrule, parent, false));
    }

    @Override
    public void onBindViewHolder(PayRuleAdapter.Vh holder, int position) {
        holder.setData(mPayRuleList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mPayRuleList.size();
    }

    class Vh extends RecyclerView.ViewHolder {
        TextView monthTextView;
        TextView moneyTextView;
        LinearLayout ll_item_payrule;

        public Vh(View itemView) {
            super(itemView);
            ll_item_payrule=itemView.findViewById(R.id.ll_item_payrule);
            monthTextView=(TextView)itemView.findViewById(R.id.tv_payrule_time);
            moneyTextView=(TextView)itemView.findViewById(R.id.tv_payrule_money);
            itemView.setOnClickListener(mOnClickListener);
        }

        @SuppressLint("StringFormatMatches")
        void setData(PayRule payRule, int position) {
            itemView.setTag(position);
            monthTextView.setText(mContext.getString(R.string.vip_payrule_month_format,payRule.getMonth()));
            moneyTextView.setText(payRule.getMoney());
            if(position==curPosition){
                ll_item_payrule.setBackgroundResource(R.drawable.vip_bg_pay_select);
            }else {
                ll_item_payrule.setBackground(null);
            }
        }
    }


    public void refresh(List<PayRule> newList) {
        mPayRuleList.removeAll(mPayRuleList);
        mPayRuleList.addAll(newList);
        notifyDataSetChanged();
    }

    public void SetSelect(int position){
        mActionListener.onItemClick(mPayRuleList.get(position));
    }


    public interface ActionListener {
        void onItemClick(PayRule payRule);
    }
}
